<template>
  <div class="list">
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>申请管理</el-breadcrumb-item>
      <el-breadcrumb-item>申请列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="searchQuery"
            clearable
            @clear="getUserList()"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getUserSearch"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <!-- <el-button type="info" plain @click="quchong">一键去重</el-button> -->
        </el-col>
      </el-row>

      <!-- 用户列表区域 -->
      <el-table :data="userlist" border stripe>
        <el-table-column align="center" type="index"></el-table-column>
        <el-table-column
          align="center"
          label="申请用户"
          prop="tel"
        ></el-table-column>
        <el-table-column align="center" label="宠物图片">
          <template slot-scope="scope">
            <img v-if="scope.row.img" class="img" :src="scope.row.img" />
            <span v-else>{{ scope.row.key }}</span>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="宠物名称"
          prop="title"
        ></el-table-column>
        <el-table-column
          align="center"
          label="宠物详情"
          prop="xq"
          width="340px"
        ></el-table-column>
        <el-table-column align="center" label="是否接种疫苗">
          <template slot-scope="scope">
            <span v-if="scope.row.num == 0">是</span>
            <span v-if="scope.row.num == 1">否</span>
            <span v-else>{{ scope.row.key }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="240px">
          <template slot-scope="scope">
            <!-- 删除按钮 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeUserById(scope.row.yuyue_id)"
            ></el-button>
            <!-- 删除按钮 -->
            <el-button
              type="warning"
              size="mini"
              v-if="scope.row.active==0"
              @click="tongguo(scope.row.yuyue_id)"
            >通过</el-button>
             <el-button
              type="info"
              size="mini"
              v-if="scope.row.active==0"
              @click="butongguo(scope.row.yuyue_id)"
            >不通过</el-button>
            <el-button
              type="success"
              size="mini"
              v-if="scope.row.active==1"
            >已通过</el-button>
              <el-button
              type="danger"
              size="mini"
              v-if="scope.row.active==2"
            >未通过</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>
<script>
export default {
  data() {
    return {
      userlist: [],
      searchQuery: "", // 搜索
      // 控制修改用户对话框的显示与隐藏
      editDialogVisible: false,
      editForm: [],
    };
  },
  created() {
    this.getUserList();
  },
  methods: {
    async getUserList() {
      const { data: res } = await this.$http.get("getYuyueAll");
      // if (res.meat.status !== 200) return this.$message.error(res.meta.msg)
      this.userlist = res.list;
      console.log(res.list);
    },

 // 删除
    async removeUserById(yuyue_id) {
      // 弹框询问用户是否删除数据
      const confirmResult = await this.$confirm(
        "是否删除?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);

      // 如果用户确认删除，则返回值为字符串 confirm
      // 如果用户取消了删除，则返回值为字符串 cancel
      // console.log(confirmResult)
      if (confirmResult !== "confirm") {
        return this.$message.info("已取消删除");
      }

      const { data: res } = await this.$http.get("delYuyue", {
        params: {
          yuyue_id,
        },
      });
      console.log(res.data);
      this.$message.success("删除成功！");
      this.getUserList();
    },


    // 通过
    async tongguo(yuyue_id) {
      const confirmResult = await this.$confirm(
        "是否确认通过?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      if (confirmResult !== "confirm") {
        return this.$message.info("已取消通过");
      }

      const { data: res } = await this.$http.get("updateActive", {
        params: {
          yuyue_id,
            active:1
        },
      });
      console.log(res.data);
      this.$message.success("通过成功！");
      this.getUserList();
    },


      // 不通过
    async butongguo(yuyue_id) {
      const confirmResult = await this.$confirm(
        "是否确认通过?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      if (confirmResult !== "confirm") {
        return this.$message.info("已取消不通过");
      }

      const { data: res } = await this.$http.get("updateActive", {
        params: {
          yuyue_id,
          active:2
        },
      });
      console.log(res.data);
      this.$message.success("操作成功！");
      this.getUserList();
    },


    // 搜索
    async getUserSearch(){
       const { data: res } = await this.$http.get('search2',{params:{
        tel:this.searchQuery
      }})
      console.log(res.list)
      this.userlist = res.list
    }

  },
};
</script>
<style lang="less" scoped>
.img {
  width: 120px;
  height: 80px;
}
</style>